package com.app.micaihu.view.main;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.bean.news.NewsEntity;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.view.lunch.LunchActivity;
import com.app.micaihu.view.main.game.GameDetailActivity;
import com.app.micaihu.view.main.game.GameRaidersNewsActivity;
import com.app.micaihu.view.main.game.GameRaidersVideoActivity;
import com.app.micaihu.view.main.topic.PostDetailActivity;
import com.app.micaihu.view.newsdetail.ImageDetailActivity;
import com.app.micaihu.view.newsdetail.NewsDetailActivity;
import com.app.micaihu.view.newsdetail.NewsTopicsActivity;
import com.app.micaihu.view.newsdetail.VideoDetailActivity;
import com.app.micaihu.view.user.userinfo.activity.MyHomepageDynamicActivity;
import com.baidu.mobstat.StatService;
import com.blankj.utilcode.util.ActivityUtils;

/**
 * Created by Guo on 2016/11/7.
 * <p>
 * <p>
 * 唤醒-分发跳转的Activity
 */
public class DispatchActivity extends BaseOldActivity {

    public static final String PUSH_OR_WAKEUP = "push_or_wakeup";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        LogUtils.e("gzq", "DispatchActivity");

        Intent intentCreate = getIntent();
        if (intentCreate == null) {
            finish();
            return;
        }

        //默认是false,走唤醒逻辑
        boolean push = intentCreate.getBooleanExtra(PUSH_OR_WAKEUP, false);


        if (push) {//推送逻辑
            try {
                if (TypeConstant.PUSHTYPE_XIAOMI.equals(intentCreate.getStringExtra(TypeConstant.PUSH_TYPE))) {
                    StatService.onEvent(this, "026", "小米推送点击", 1);// 统计的点击的次数
                } else if (TypeConstant.PUSHTYPE_UMENG.equals(intentCreate.getStringExtra(TypeConstant.PUSH_TYPE))) {
                    StatService.onEvent(this, "027", "友盟推送点击", 1);// 统计的点击的次数

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            //当前栈中没有MainActivity,则去开屏

//                LogUtils.e("gzq", "直接进行跳转");

            String articleId = intentCreate.getStringExtra(Config.Extra.PARAMETER1);
            String articleType = intentCreate.getStringExtra(Config.Extra.PARAMETER2);
            String pushType = intentCreate.getStringExtra(TypeConstant.PUSH_TYPE);

            String jumpUrl = intentCreate.getStringExtra("url");
            String serialId = intentCreate.getStringExtra("serialId");


            NewsEntity newsEntity = new NewsEntity();
            newsEntity.setArticleId(articleId);
            newsEntity.setArticleType(articleType);

            //设置jumpUrl
            newsEntity.setJumpUrl(jumpUrl);
            newsEntity.setSerialId(serialId);


            //统计事件
            switchEvent(articleType);

            TypeConstant.openTypeDetail(newsEntity, this, pushType);


        } else {//唤醒逻辑

            Uri uri = intentCreate.getData();
            if (uri != null && "webtomicai".equals(uri.getScheme())) {
                String articleId = uri.getQueryParameter("id");
                String articleType = uri.getQueryParameter("type");

                if (!TextUtils.isEmpty(articleType)) {

                    Intent intent = new Intent();
                    intent.putExtra(Config.Extra.PARAMETER1, articleId);
                    intent.putExtra(Config.Extra.PARAMETER2, TypeConstant.SOURCETYPE_WEB);

                    switch (articleType) {
                        case TypeConstant.WAKE_UP_MICAIHU_APP:
                            intent.setClass(this, LunchActivity.class);
                            break;
                        case TypeConstant.WAKE_UP_NEWS:
                            intent.setClass(this, NewsDetailActivity.class);
                            break;
                        case TypeConstant.WAKE_UP_VIDEO:
                        case TypeConstant.WAKE_UP_ORIGNAL:
                            intent.setClass(this, VideoDetailActivity.class);
                            break;
                        case TypeConstant.WAKE_UP_TOPIC:
//                        intent.setClass(this, TopicDetailActivity.class);
                            intent.setClass(this, PostDetailActivity.class);
                            break;

                        case TypeConstant.WAKE_UP_PIC:
                            intent.setClass(this, ImageDetailActivity.class);
                            break;
                        case TypeConstant.WAKE_UP_SPECIAL:
                            intent.setClass(this, NewsTopicsActivity.class);
                            break;
                        case TypeConstant.WAKE_UP_USERHOME:
                            intent.setClass(this, MyHomepageDynamicActivity.class);
                            break;
                        case TypeConstant.WAKE_UP_GAME://游戏
                            intent.setClass(this, GameDetailActivity.class);
                            break;
                        case TypeConstant.WAKE_UP_GAME_TXT://游戏图文攻略
                            intent.setClass(this, GameRaidersNewsActivity.class);
                            String gameid = uri.getQueryParameter("gameId");
                            intent.putExtra(Config.Extra.PARAMETER2, gameid);
                            break;
                        case TypeConstant.WAKE_UP_GAME_VIDEO://游戏视频攻略
                            intent.setClass(this, GameRaidersVideoActivity.class);
                            String gameid1 = uri.getQueryParameter("gameId");
                            intent.putExtra(Config.Extra.PARAMETER2, gameid1);
                            break;
                        default:
                            intent.setClass(this, LunchActivity.class);
                            break;
                    }

                    //当前栈中没有MainActivity,则去开屏
                    if (!ActivityUtils.isActivityExistsInStack(MainActivity.class)) {
                        articleType = getArticleType(articleType);
                        if (TextUtils.equals(articleType, TypeConstant.GAME_RAIDERS_TEXT) || TextUtils.equals(articleType, TypeConstant.GAME_RAIDERS_VIDEO)) {
                            intent.putExtra("gameid", intent.getStringExtra(Config.Extra.PARAMETER2));
                        }
                        intent.putExtra(Config.Extra.PARAMETER2, articleType);
                        intent.setClass(this, LunchActivity.class);
                    }
                    if (intent.getComponent() != null) {
                        startActivity(intent);
                    }

                }
            }
        }


        finish();
    }

    /**
     * 网页跳转类型转化为文章跳转类型
     *
     * @param type
     * @return
     */
    private String getArticleType(String type) {
        String articleType;
        switch (type) {
            case TypeConstant.WAKE_UP_MICAIHU_APP:
                articleType = "";
                break;
            case TypeConstant.WAKE_UP_NEWS:
                articleType = TypeConstant.NEWS_NORMAL;
                break;
            case TypeConstant.WAKE_UP_VIDEO:
            case TypeConstant.WAKE_UP_ORIGNAL:
                articleType = TypeConstant.VIDEO_NORMAL;
                break;
            case TypeConstant.WAKE_UP_TOPIC:
                articleType = TypeConstant.POST_2052;
                break;

            case TypeConstant.WAKE_UP_PIC:
                articleType = TypeConstant.PIC_NORMAL;
                break;
            case TypeConstant.WAKE_UP_SPECIAL:
                articleType = TypeConstant.NEWS_TOPIC;
                break;
            case TypeConstant.WAKE_UP_USERHOME:
                articleType = TypeConstant.PERSONAL_HOMEPAGE;
                break;
            case TypeConstant.WAKE_UP_GAME://游戏
                articleType = TypeConstant.GAME_VIDEO;
                break;
            case TypeConstant.WAKE_UP_GAME_TXT://游戏图文
                articleType = TypeConstant.GAME_RAIDERS_TEXT;
                break;
            case TypeConstant.WAKE_UP_GAME_VIDEO://游戏视频
                articleType = TypeConstant.GAME_RAIDERS_VIDEO;
                break;
            default:
                articleType = "";
                break;
        }
        return articleType;
    }


    /**
     * 为友盟点击 统计时间
     *
     * @param articleType
     */
    private void switchEvent(String articleType) {
        if (TextUtils.isEmpty(articleType)) return;
        switch (articleType) {
            case TypeConstant.NEWS_NORMAL:
            case TypeConstant.NEWS_WEB:
            case TypeConstant.NEWS_SHOP:

                StatService.onEvent(this, "054", "新闻", 1);// 统计的点击的次数
                break;
            case TypeConstant.NEWS_SHOP_WEB://电商网页文章

                StatService.onEvent(this, "054", "电商软文", 1);// 统计的点击的次数
                break;
            //适配图库
            case TypeConstant.PIC_NORMAL:

                StatService.onEvent(this, "054", "图库", 1);// 统计的点击的次数
                break;

            //适配新闻专题
            case TypeConstant.NEWS_TOPIC:

                StatService.onEvent(this, "054", "新闻专题", 1);// 统计的点击的次数
                break;
            //视频
            case TypeConstant.VIDEO_ORIGINAL:
            case TypeConstant.VIDEO_NORMAL:

                StatService.onEvent(this, "054", "视频", 1);// 统计的点击的次数
                break;

            case TypeConstant.POST_2052:
                StatService.onEvent(this, "054", "话题", 1);// 统计的点击的次数
                break;

            //自定义广告
            case TypeConstant.AD:

                StatService.onEvent(this, "054", "广告", 1);// 统计的点击的次数
                break;
            case TypeConstant.PUSH_COMMENT://推送的评论

                StatService.onEvent(this, "054", "评论点击", 1);// 统计的点击的次数
                break;

            case TypeConstant.GAME_VIDEO://游戏视频

                StatService.onEvent(this, "054", "游戏视频点击跳转内页", 1);// 统计的点击的次数
                break;
            case TypeConstant.GAME_RAIDERS_TEXT://游戏攻略

                StatService.onEvent(this, "054", "游戏图文攻略点击", 1);// 统计的点击的次数
                break;
            case TypeConstant.GAME_RAIDERS_VIDEO://游戏攻略

                StatService.onEvent(this, "054", "游戏图文攻略点击", 1);// 统计的点击的次数
                break;
            case TypeConstant.PERSONAL_HOMEPAGE://个人主页
                StatService.onEvent(this, "054", "个人主页", 1);// 统计的点击的次数
                break;
            case TypeConstant.ONLY_URL://推送网页

                StatService.onEvent(this, "054", "只有url", 1);// 统计的点击的次数
                break;
        }

    }

}
